<template>
    <div class="bd">
        <el-form label-width="70px" class="login-container" ref="ruleForm" :model="ruleForm" :rules="rules">
            <h3>超市仓库管理系统</h3>
            <el-form-item label="用户名" prop="name">
                <el-input v-model="ruleForm.name" placeholder="请输入用户名"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input type="password" v-model="ruleForm.password" placeholder="请输入密码"></el-input>
            </el-form-item>
            <a href="http://localhost:7070/#/register" id="link">没有账号？去注册</a>
            <el-form-item>
                <el-button style="width: 130px;" type="primary" @click="submitForm('ruleForm')">登录</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
    import md5 from 'js-md5'
    import Cookie from 'js-cookie'
    import { Login } from '@/api/index.js'
    export default {
        data() {
            return {
                ruleForm: {
                    name: '',
                    password: '',
                },
                rules: {
                    name: [
                        { required: true, message: '请输入用户名', trigger: 'blur' },
                        { min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        { min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur' }
                    ],
                }
            }
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    // 规则校验通过
                    if (valid) {
                        // 登录密码加密
                        let pw = md5(this.ruleForm.password + "lixiaoyu")
                        Login(this.ruleForm.name, pw).then((res) => {
                            // 登录成功
                            if (res.data.code === 200) {
                                Cookie.set('token', res.data.data[0].token)
                                Cookie.set('user', JSON.stringify(res.data.data[1].user))
                                this.$message({
                                    message: '登录成功',
                                    type: 'success'
                                });
                                this.$router.push('/home')
                            } else {
                                // 登录失败
                                this.$message.error(res.data.msg);
                            }

                        })
                    } else {
                        // 规则校验失败
                        this.$message.error("请输入正确的用户名、密码！");
                        return false;
                    }
                });
            },
        }
    }
</script>
<style lang="less" scoped>
    .bd {
        background-image: url('../assets/images/login_img.jpg');
        background-repeat: no-repeat;
        background-size: cover;
        background-position-y: center;
        position: fixed;
        height: 100%;
        width: 100%;
    }

    .login-container {
        width: 350px;
        border: 1px solid #eaeaea;
        margin: 0 auto;
        margin-top: 25vh;
        padding: 35px 35px 15px 35px;
        background-color: #fff;
        border-radius: 15px;
        box-sizing: border-box;

        h3 {
            text-align: center;
            margin-bottom: 40px;
        }

        .el-input {
            width: 198px;
        }
        #link{
            font-size: 12px;
            margin-left: 180px;
        }
    }
</style>